<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="org.jbpm.JbpmContext"%>
<%@page import="org.jbpm.JbpmConfiguration"%>
<%@page import="org.jbpm.db.GraphSession"%>
<%@page import="org.jbpm.graph.exe.ProcessInstance"%>
<%@page import="org.hibernate.Session"%>
<%@page import="org.hibernate.Criteria"%>
<%@page import="org.jbpm.taskmgmt.exe.TaskInstance"%>
<%@page import="org.hibernate.criterion.Restrictions"%>
<%@page import="org.hibernate.criterion.Projections"%>
<%@page import="org.hibernate.criterion.Expression"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title> 任务实例查询  </title>   

  </head>
  
  <body>
  <form action="listTaskInstanceQuery.jsp" method="post">
		<table width="90%" cellspacing="1" bgcolor="#6495ED">
			<tr bgcolor="white">
				<th >
					流程名称：<input type="text" name="pdName" >
					任务名称：<input type="text" name="taskName" >
					参与者：<input type="text" name="actorId" > 
					处理状态：<select name="tstate">
										<option value="-1">所有状态</option>
										<option value="0">刚创建</option>
										<option value="1">任务已经启动</option>
										<option value="2">任务已完成</option>
									 </select>
					<input type="submit" value="查询" >
				</th>
			</tr>
		</table>
	</form>
  	<%
  	JbpmContext ctx = JbpmConfiguration.getInstance().createJbpmContext();
  	
  	String pdName = request.getParameter("pdName");
  	String taskName = request.getParameter("taskName");
  	String actorId = request.getParameter("actorId");
  	
  	String tstate = request.getParameter("tstate");
  	
  	Session hsession = ctx.getSession();//获取hibernate的session对象
  	Criteria crti = hsession.createCriteria(TaskInstance.class);
  	
  	if(tstate!=null){
  		if("0".equals(tstate)){//刚创建
  			crti.add(Expression.and(Expression.isNull("start"),Expression.isNull("end")));
  		}
  		if("1".equals(tstate)){//任务已经启动
  			crti.add(Expression.and(Expression.isNotNull("start"),Expression.isNull("end")));
  		}
  		if("2".equals(tstate)){//任务已完成
  			crti.add(Expression.and(Expression.isNotNull("start"),Expression.isNotNull("end")));
  		}
  	}
  	
  	if(pdName!=null && !"".equals(pdName)&& !"null".equals(pdName)){
  		crti.createAlias("processInstance","pi").createAlias("pi.processDefinition","pd").add(Restrictions.eq("pd.name",pdName));
  	}
  	
  	if(taskName!=null && !"".equals(taskName)&& !"null".equals(taskName)){
  		crti.add(Restrictions.eq("name",taskName));
  	}
  	
  	if(actorId!=null && !"".equals(actorId)&& !"null".equals(actorId)){
  		crti.add(Restrictions.eq("actorId",actorId));
  	}
  	
  	int total = ((Integer)crti.setProjection(Projections.countDistinct("id")).uniqueResult()).intValue();
  	crti.setProjection(null);
  	String pageNum = request.getParameter("pageNum");
  	pageNum = pageNum==null || "".equals(pageNum)?"1":pageNum;
  	int targetPage = Integer.parseInt(pageNum);
  	int everyPage = 5;
  	int startNum = (targetPage-1)*everyPage;
  	
  	int totalPage = (total+everyPage-1)/everyPage;
  	
  	crti.setMaxResults(everyPage);
  	crti.setFirstResult(startNum);
  	
  	List list =crti.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
  	pageContext.setAttribute("taskInstances",list);
  	
  	 %>
  	 <c:if test="${empty taskInstances}">
  	 	没有任务
  	 </c:if>
  	  <c:if test="${!empty taskInstances}">  	 	
	  	 <table width="90%" cellspacing="1" bgcolor="#6495ED">
	  	 	<tr bgcolor="#2F4F4F">
	  	 		<th><font color="white">任务名称</font></th>
	  	 		<th><font color="white">创建时间</font></th>
	  	 		<th><font color="white">开始时间</font></th>
	  	 		<th><font color="white">结束时间</font></th>
	  	 		<th><font color="white">actor</font></th>
	  	 		<th><font color="white">流程跟踪</font></th>
	  	 	</tr>
	  	 	
	  	 	<c:forEach items="${taskInstances}" var="t" varStatus="s">
	  	 		<tr bgcolor="white">
	  	 			<td><a href="${pageContext.request.contextPath}/jbpm/openTask.jsp?tiid=${t.id}">${t.name }</a></td>
	  	 			<td>${t.create }</td>
	  	 			<td>${t.start }</td>
	  	 			<td>${t.end }</td>
	  	 			<td>${t.actorId }</td>
	  	 			<td><a href="${pageContext.request.contextPath }/processInstanceImage.jsp?tokenId=${t.token.id}">流程跟踪</a></td>
	  	 		</tr>
	  	 		
	  	 	</c:forEach>
	  	 	<tr bgcolor="white">
	  	 			<td colspan="6">
	  	 				共<%=total %>条,<%=totalPage %>页  当前第<%=pageNum %>页
	  	 				<%
	  	 					for(int i = 1;i<=totalPage;i++){
	  	 						%>
	  	 						<a href="listTaskInstanceQuery.jsp?pageNum=<%=i %>&pdName=<%=pdName %>&taskName=<%=taskName %>&actorId=<%=actorId %>"><%=i %></a>	
	  	 						<%
	  	 					}
	  	 				 %>
	  	 				
	  	 			</td>
	  	 		</tr>
	  	 </table>  	 
  	 </c:if>
  </body>
</html>
